// pages/list/list.js
var app = getApp();
var url = app.globalData.url;
// var headers = app.globalData.headers;
// var city, dest_id, dest_level, time_start, time_end, days, sort_type, main_type, next_page_token,req;
var headers,mainId, thirdId, name;
Page({
  data:{
    date: {
      start:"",
      end:""
    },
    url:"https://app1.sztravel.com.cn/file/weixinapp/img",
    key:{
      mask1:false,
      mask2:false,
      mask3:false,
    },
    list:[{
      url:"icon-goup",
      tip:"↑",
      isclick:false,
      mes:"价格最低开始排序",
      sortNum:1
    },
    {
      url:"icon-godown",
      tip:"↓",
      isclick:false,
      mes:"价格最高开始排序",
      sortNum:2
    },
    {
      url:"icon-day",
      tip:"↑",
      isclick:false,
      mes:"最近的出发日期排序",
      sortNum:3
    },
    {
      url:"icon-day",
      tip:"↓",
      isclick:false,
      mes:"最远的出发日期排序",
      sortNum:4
    },
    {
      url:"icon-day01",
      tip:"↑",
      isclick:false,
      mes:"最少的行程天数排序",
      sortNum:5
    },
    {
      url:"icon-day01",
      tip:"↓",
      isclick:false,
      mes: "最多的行程天数排序",
      sortNum:6
    }],
    city_selected:"",
    days_selected:0,
    lineList:[],//线路列表
    showLoading:true,//控制数据加载提示开关
    empty:false,//是否为空
    re:{//初始参数
      city:"深圳",
      dest_id:0,
      dest_level:0,
      time_start:"",
      time_end:"",
      days:0,
      sort_type:0,
      main_type:0,
      next_page_token:""
    },
    destination: {
      bdata: [],
      idx: 0,
      rightList: [],
      mainId: 1
    }
  },
  //一下目的地
  search: function (e) {
    console.log(e)
    var mainId = e.currentTarget.dataset.mainid;
    var thirdId = e.currentTarget.dataset.thirdid;
    var name = e.currentTarget.dataset.city;
    wx.redirectTo({
      url: '../../pages/list/list?mainId=' + mainId + '&thirdId=' + thirdId + '&name=' + name + '',
    })
    // Router.navto({
    //   url:"../../pages/list/list",
    //   data:{
    //     city:e.target.dataset.city,
    //     page:"目的地"
    //   }
    // })
  },
  select: function (e) {
    var destination = this.data.destination;
    var dmainId = destination.bdata[e.target.dataset.i].id
    destination.mainId = dmainId;
    destination.idx = e.target.dataset.i;
    this.setData({
      destination: destination
    })

    this.showRight(dmainId)
  },
  showRight: function (mainId) {
    var destination = this.data.destination;
    let that = this
    wx.request({
      url: url + 'api/get_destination_list',
      header: headers,
      data: {
        "main_type_id": mainId
      },
      success: function (data) {
        if (data.data.result === 1) {
          destination.rightList = data.data.data
          that.setData({
            destination: destination
          })
        } else {
          wx.showModal({
            title: '来自伴行的提示',
            content: data.data.message,
          })
        }

      }
    })
  },
  Load: function () {
    var that = this;
    let mainId;
    var destination = this.data.destination
    try {
      var value = JSON.parse(wx.getStorageSync('mainPlace'));
      destination.bdata = value
      if (value) {
        // Do something with return value
        that.setData({
          destination: destination
        })
      }
    } catch (e) {
      // Do something when catch error
    };

    mainId = destination.bdata[0].id;

    this.showRight(mainId)
  },


















 // 以上目的地
  changeDate:function(){//日期筛选
    var re=this.data.re;
    var date=this.data.date;
    var days = this.data.days_selected;
    var that=this
    if(date.start && date.end){
      if (parseInt(date.start.split("-").join("")) > parseInt(date.end.split("-").join(""))){
        that.showMode("请填写正确的日期期间");
        return;
      }
    
    }
    re.time_start=date.start;
    re.time_end=date.end;
    re.days=days;
    re.next_page_token="";
    this.setData({
      re:re,
      key: {
        mask1: false,
        mask2: !this.data.key.mask2,
        mask3: false
      },
      lineList: []
    });
    this.request(true);
  },
  //时间加减控制
  changedays(e){
    var days = this.data.days_selected;
    if(e.target.dataset.i==0){
      days++
      this.setData({
        days_selected:days
      })
    }else if(e.target.dataset.i==1){
      days--
      if(days<0){
        days = 0;
      }
      this.setData({
        days_selected:days
      })
    }
    console.log(this.data.days_selected)
  },
  // //选择城市控制
  // city_select(e){
  //   this.setData({
  //     city_selected:this.data.item[e.target.dataset.i]
  //   })
  //   console.log(this.data.city_selected)
  // },
  // //弹出层逻辑控制
  // //目的地弹出层开关
  destination:function(){ 
    this.setData({
      key:{
        mask1:!this.data.key.mask1,
        mask2:false,
        mask3:false,
      },
      showLoading: true
    })
    
  },
  //  //日期弹出层开关
  date:function(){
    this.setData({
      key:{
        mask1:false,
        mask2:!this.data.key.mask2,
        mask3:false,
      },
      showLoading: true
    })
  },
  //  //筛选弹出层开关
  fillter:function(e){
    
   
    
    this.setData({
      key:{
        mask1:false,
        mask2:false,
        mask3:!this.data.key.mask3,
      },
      showLoading:true
      
    })
    
  },
  // //选择筛选项目
  selectL:function(e){
    var re = this.data.re;
    re.next_page_token="";
    var sortType = e.currentTarget.dataset.sorttype;    
    re.sort_type = sortType;
    this.setData({
      key: {
        mask1: false,
        mask2: false,
        mask3: !this.data.key.mask3,
      },
      re:re,
      lineList:[]
    })
    
    
    this.request(true);


    // var index = e.target.dataset.li;
    // var cdata = this.data.list;
    // for(let i=0;i<cdata.length;i++){
    //   cdata[i].isclick=false;
    // }
    // cdata[index].isclick=!this.data.list[index].isclick;
    // let cdata2 = this.data.key;
    // cdata2.mask3 = false;
    // this.setData({
    //   list:cdata,
    //   key:cdata2
    // })
    
  },
  navto:function(e){ 
    wx.navigateTo({
      url: '../line_detail/line_detail?lineId='+e.currentTarget.dataset.id+'',
    });
  },
  // //弹出层结束
  // //跳转到路线详情页面
  // navto(){
  //   wx.navigateTo({
  //     url: '../../pages/line_detail/line_detail',
  //     success: function(res){
  //       // success
  //     },
  //     fail: function() {
  //       // fail
  //     },
  //     complete: function() {
  //       // complete
  //     }
  //   })
  // },
  // startPageForData(data){
  //   console.log(data);
  // },
  // bindPickerChange: function(e) {
  //   console.log('picker发送选择改变，携带值为', e.detail.value)
  //   this.setData({
  //     index: e.detail.value
  //   })
  // },
  showMode:function(mes){
    wx.showModal({
      title: '来自伴行的提示',
      content: mes,
      showCancel:false
    })
  },
  bindDateChange: function(e) {
    var op,ed;
    var typ=e.currentTarget.dataset.typ;
    var date=this.data.date;
    var that=this;
    console.log(date);
    var start=date.start;
    var end=date.end;
    if (!start && !end) {//先输入开始

    } else if (start && !end){//已经存在开始 
        if(typ=="end"){//正要输入结束
          op = parseInt(date.start.split("-").join(""));
          ed = parseInt(e.detail.value.split("-").join(""));
          console.log(op)
          console.log(ed)
          if(ed<op){//结尾日期小
            that.showMode("截止日期不可早于" + date.start)
          }
        }
    } else if (!start && end){//先输入截止
      if (typ == "start") {//正要输入开始
        op = parseInt(e.detail.value.split("-").join(""));
        ed = parseInt(date.end.split("-").join(""));
        console.log(op)
        console.log(ed)
        if (ed < op) {//结尾日期小
          that.showMode("截止日期不可晚于" + date.end)
        }
      }
    } else if (start && end){//更改日期
      if (typ == "start") {//正要输入结束
        op = parseInt(e.detail.value.split("-").join(""));
        ed = parseInt(date.end.split("-").join(""));
        console.log(op)
        console.log(ed)
        if (ed < op) {//结尾日期小
          that.showMode("截止日期不可晚于" + date.end)
        }
      } else if(typ == "end"){//正要输入结束
        op = parseInt(date.start.split("-").join(""));
        ed = parseInt(e.detail.value.split("-").join(""));
        console.log(op)
        console.log(ed)
        if (ed < op) {//结尾日期小
          that.showMode("截止日期不可早于" + date.start)
        }
      }
    };
    if(typ=="start"){
      date.start=e.detail.value;
    }else if(typ=="end"){ 
      date.end = e.detail.value;
    }
    this.setData({
      date: date
    })
  },
  // showList:function(){
  //   console.log(req);
  //   console.log(JSON.stringify(req))
  //   let encodeReq=encodeURIComponent(JSON.stringify(req));
  //   console.log(encodeReq)
  //   console.log(headers)
  //   wx.request({
  //     url: url +'api/get_lines_by_condition',
  //     Header:headers,
  //     data:{
  //       req: encodeReq
  //     },
  //     success:function(data){
  //       console.log(data)
  //     }
  //   })
  // },
  // 动态设置标题栏
  onLoad:function(options){
    mainId = parseInt(options.mainId);
    thirdId = parseInt(options.thirdId);
     name=options.name; 
    //  mainId = 2;
    this.Load()
    







    // console.log(options.mainId)
    // city="深圳";
    // dest_id=0;
    // dest_level=0;
    // time_start="";
    // time_end="";
    // days=0;
    // sort_type=0;
    // main_type = parseInt(options.mainId);
    // next_page_token="";
    // req = {
    //   city: city,
    //   dest_id: dest_id,
    //   dest_level: dest_level,
    //   time_start: time_start,
    //   time_end: time_end,
    //   days: days,
    //   sort_type: sort_type,
    //   main_type: main_type,
    //   next_page_token: next_page_token
    // };
    // this.setData({
    //   req:req
    // })
    // this.showList();
    
    // if(options.item){
    //   wx.setNavigationBarTitle({
    //     title: options.item,
    //     success: function(res) {
    //       // success
    //     }
    //   })
    // }
  },
  request:function(flag){
    var re=this.data.re;//参数
    var that=this;
    var lineList = this.data.lineList
    console.log(re)
    var request = '{"city":"' 
    + re.city + '","dest_id":' 
    + re.dest_id + ',"dest_level":' 
    + re.dest_level + ',"time_start":"' 
    + re.time_start + '","time_end":"' 
    + re.time_end+'","days":'
    + re.days + ',"sort_type":' 
    + re.sort_type+',"main_type":' 
    + re.main_type + ',"next_page_token":"' 
    + re.next_page_token+'"}'
    console.log(request);  
    wx.request({
      url: url +'api/get_lines_by_condition',
      method:"POST",
      header:headers,
      data:{
        "req":encodeURIComponent(request)
      },
      success:function(data){
        console.log(data);
        if(data.data.result===1){
          if(flag){//是否为空
              var empty;
            if (!data.data.data.line_list){//空
              empty=true
            }else{
              empty=false
            }
          }
          lineList.push.apply(lineList, data.data.data.line_list);
          re.next_page_token = data.data.data.next_page_token;
          if (data.data.data.next_page_token){
            var loadingFlag=true
          }else{
            var loadingFlag = false
          }
          that.setData({
            showLoading: loadingFlag,
            lineList:lineList,
            re:re,
            empty: empty
          })
          console.log(that.data)
        }else{
          wx.showModal({
            title: '来自伴行的提示',
            content:data.data.message,
            showCancel:false
          })
        }
      }
    })


  },
  onReachBottom:function(){//到底部加载
      var re=this.data.re;
      var that=this
      if (re.next_page_token){//存在下一页
        that.request();
      }
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
    var that=this
    headers = app.globalData.headers;
    var re=this.data.re;
    if (mainId && thirdId){//目的地列表进来
      re.main_type = mainId;
      re.dest_id = thirdId;
      re.dest_level=3;
      
      that.setData({
        re:re
      })
    }else{//首页等进来
      re.main_type = mainId;
      
      that.setData({
        re: re
      })
    }
    this.request(true);
    
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})